home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1998 July / EnigmA AMIGA RUN 29 (1998)(G.R. Edizioni)(IT)[!][issue 1998-07 & 08].iso / earcd / phase5 / lha-ppc / src / lhlist.c < prev    next >
C/C++ Source or Header  |  1997-12-04  |  7KB  |  294 lines

  1. /*----------------------------------------------------------------------*/
  2. /*        LHarc List Command                    */
  3. /*        This is part of LHarc UNIX Archiver Driver        */
  4. /*                                    */
  5. /*        Copyright(C) MCMLXXXIX  Yooichi.Tagawa            */
  6. /*                                    */
  7. /*  V0.00  Original                1988.05.23  Y.Tagawa    */
  8. /*  V1.00  Fixed                1989.09.22  Y.Tagawa    */
  9. /*  V1.01  Bug Fix for month name        1989.12.25  Y.Tagawa    */
  10. /*----------------------------------------------------------------------*/
  11.  
  12. #include "lharc.h"
  13.  
  14. static long packed_size_total;
  15. static long original_size_total;
  16. static int list_files;
  17.  
  18. /*----------------------------------------------------------------------*/
  19. /*                Print Stuff                */
  20. /*----------------------------------------------------------------------*/
  21.  
  22. /* need 14 or 22 (when verbose_listing is TRUE) column spaces */
  23. static void print_size(long packed_size, long original_size)
  24. {
  25.   if (verbose_listing)
  26.     printf ("%7d ", packed_size);
  27.  
  28.   printf ("%7d ", original_size);
  29.  
  30.   if (original_size == 0L)
  31.     printf ("******");
  32.   else
  33.     printf ("%3d.%1d%%",
  34.         (int)((packed_size * 100L) / original_size),
  35.         (int)((packed_size * 1000L) / original_size) % 10);
  36. }
  37.  
  38. /* need 12 or 17 (when verbose_listing is TRUE) column spaces */
  39. static void print_stamp(time_t t)
  40. {
  41.   static boolean got_now = FALSE;
  42.   static time_t now;
  43.   static unsigned int threshold;
  44.   static char t_month[12*3+1] = "JanFebMarAprMayJunJulAugSepOctNovDec";
  45.   struct tm *p;
  46.  
  47.   if (t == 0)
  48.     {
  49.     printf ("            "); /* 12 spaces */
  50.       return;
  51.     }
  52.  
  53.   if (!got_now)
  54.     {
  55.       now = time ((time_t*)0);
  56.       p = localtime (&now);
  57.       threshold = p->tm_year * 12 + p->tm_mon - 6;
  58.       got_now = TRUE;
  59.     }
  60.  
  61.   p = localtime (&t);
  62.  
  63.   if (p->tm_year * 12 + p->tm_mon > threshold)
  64.     printf ("%.3s %2d %02d:%02d",
  65.         &t_month[p->tm_mon * 3], p->tm_mday, p->tm_hour, p->tm_min);
  66.   else
  67.     printf ("%.3s %2d  %04d",
  68.         &t_month[p->tm_mon * 3], p->tm_mday, p->tm_year + 1900);
  69. }
  70.  
  71. static void print_bar(void)
  72. {
  73.   char *p, *q;
  74.   /* 17+1+(0 or 7+1)+7+1+6+1+(0 or 1+4)+(12 or 17)+1+20 */
  75.   /*       12345678901234567_  1234567_123456  _123456789012   1234 */
  76.  
  77.   if (verbose_listing)
  78.     {
  79.       p = "- ------ ---------- ";
  80.       q = " -------------";
  81.     }
  82.   else
  83.     {
  84.       p = " ";
  85.       q = " --------------------";
  86.     }
  87.  
  88.   if (verbose)
  89.     q = "";
  90.  
  91.   printf ("----------------- ------- ------%s------------%s\n", p, q);
  92. }
  93.  
  94.  
  95. /*----------------------------------------------------------------------*/
  96. /*                                    */
  97. /*----------------------------------------------------------------------*/
  98.  
  99. static void list_header(void)
  100. {
  101.   char *p, *q;
  102.  
  103.   if (verbose_listing)
  104.     {
  105.       p = "PACKED    SIZE  RATIO        CRC   ";
  106.       q = "       NAME";
  107.     }
  108.   else
  109.     {
  110.       p = "  SIZE  RATIO";
  111.       q = "    NAME";
  112.     }
  113.  
  114.   if (verbose)
  115.     q = "";
  116.  
  117.   printf (" PERMSSN  UID GID  %s     STAMP%s\n", p, q);
  118. #if 0
  119.   printf (" PERMSSN  UID GID %s   SIZE  RATIO%s %s    STAMP%s%s\n",
  120.       verbose_listing ? " PACKED " : "",    /* 8,0 */
  121.       verbose_listing ? "  CRC" : "",    /* 5,0 */
  122.       verbose_listing ? "  " : "",        /* 2,0 */
  123.       verbose_listing ? "      " : "   ",        /* 6,3 */
  124.       verbose ? "" : " NAME");
  125. #endif
  126.   print_bar ();
  127. }
  128.  
  129. static void list_one(LzHeader *hdr)
  130. {
  131.   register int mode;
  132.   register char *p;
  133.   char method[6];
  134.  
  135.   if (verbose)
  136.     printf ("%s\n", hdr->name);
  137.  
  138.   strncpy(method,hdr->method,5);
  139.   method[5]='\0';
  140.  
  141.   switch ( mode=hdr->extend_type )
  142.   {
  143.       case EXTEND_UNIX:
  144.         mode=hdr->unix_mode;
  145.       printf ("%c%c%c%c%c%c%c%c%c%4d/%-4d",
  146.           ((mode & UNIX_OWNER_READ_PERM)  ? 'r' : '-'),
  147.           ((mode & UNIX_OWNER_WRITE_PERM) ? 'w' : '-'),
  148.           ((mode & UNIX_OWNER_EXEC_PERM)  ? 'x' : '-'),
  149.           ((mode & UNIX_GROUP_READ_PERM)  ? 'r' : '-'),
  150.           ((mode & UNIX_GROUP_WRITE_PERM) ? 'w' : '-'),
  151.           ((mode & UNIX_GROUP_EXEC_PERM)  ? 'x' : '-'),
  152.           ((mode & UNIX_OTHER_READ_PERM)  ? 'r' : '-'),
  153.           ((mode & UNIX_OTHER_WRITE_PERM) ? 'w' : '-'),
  154.           ((mode & UNIX_OTHER_EXEC_PERM)  ? 'x' : '-'),
  155.           hdr->unix_uid, hdr->unix_gid);
  156.       break;
  157.     case EXTEND_OS68K:
  158. /**/    case EXTEND_XOSK: /**/
  159.         mode=hdr->unix_mode;
  160.       printf ("%c%c%c%c%c%c%c%c %4d/%-4d",
  161.           ((mode & OSK_DIRECTORY_PERM)    ?    'd' : '-'),
  162.           ((mode & OSK_SHARED_PERM)     ?     's' : '-'),
  163.           ((mode & OSK_OTHER_EXEC_PERM)    ?     'e' : '-'),
  164.           ((mode & OSK_OTHER_WRITE_PERM)?     'w' : '-'),
  165.           ((mode & OSK_OTHER_READ_PERM)    ?     'r' : '-'),
  166.           ((mode & OSK_OWNER_EXEC_PERM)    ?     'e' : '-'),
  167.           ((mode & OSK_OWNER_WRITE_PERM)?     'w' : '-'),
  168.           ((mode & OSK_OWNER_READ_PERM)    ?     'r' : '-'),
  169.           hdr->unix_uid, hdr->unix_gid);
  170.       break;
  171.     default:
  172.       switch (hdr->extend_type)
  173.         {            /* max 18 characters */
  174.         case EXTEND_GENERIC:p = "[generic]"; break;
  175.         case EXTEND_CPM:    p = "[CP/M]"; break;
  176.         case EXTEND_FLEX:    p = "[FLEX]"; break;
  177.         case EXTEND_OS9:    p = "[OS-9]"; break;
  178.         case EXTEND_OS68K:    p = "[OS-9/68K]"; break;
  179.         case EXTEND_MSDOS:    p = "[MS-DOS]"; break;
  180.         case EXTEND_MACOS:    p = "[Mac OS]"; break;
  181.         case EXTEND_OS2:    p = "[OS/2]"; break;
  182.         case EXTEND_HUMAN:    p = "[Human68K]"; break;
  183.         case EXTEND_OS386:    p = "[OS-386]"; break;
  184.         case EXTEND_RUNSER:    p = "[Runser]"; break;
  185. #ifdef EXTEND_TOWNSOS
  186.           /* This ID isn't fixed */
  187.         case EXTEND_TOWNSOS:    p = "[TownsOS]"; break;
  188. #endif
  189.           /* Ouch!  Please customize it's ID.  */
  190.         default:         p = "[unknown]"; break;
  191.         }
  192.       printf ("%-18.18s", p);
  193.       break;
  194.     }
  195.   
  196.   print_size (hdr->packed_size, hdr->original_size);
  197.  
  198.   if (verbose_listing)
  199.     if (hdr->has_crc)
  200.       printf (" %s %04x", method,hdr->crc);
  201.     else
  202.       printf (" %s ****",method);
  203.   
  204.   printf (" ");
  205.   print_stamp (hdr->unix_last_modified_stamp);
  206.  
  207.   if (!verbose)
  208.     printf (" %s", hdr->name);
  209.  
  210.   printf ("\n");
  211. }
  212.  
  213. static void list_tailer(void)
  214. {
  215.   struct stat stbuf;
  216.  
  217.   print_bar ();
  218.  
  219.   printf (" Total %4d file%c ",
  220.       list_files, (list_files == 1) ? ' ' : 's');
  221.   print_size (packed_size_total, original_size_total);
  222.   printf (" ");
  223.  
  224.   if (verbose_listing)
  225.     printf ("           ");
  226.  
  227.   if (stat (archive_name, &stbuf) < 0)
  228.     print_stamp ((time_t)0);
  229.   else
  230.     print_stamp (stbuf.st_mtime);
  231.  
  232.   printf ("\n");
  233. }
  234.  
  235. /*----------------------------------------------------------------------*/
  236. /*        LIST COMMAND MAIN                    */
  237. /*----------------------------------------------------------------------*/
  238.  
  239. void
  240. cmd_list ()
  241. {
  242.   FILE *afp;
  243.   LzHeader hdr;
  244.   int i;
  245.  
  246.   /* initialize total count */
  247.   packed_size_total = 0L;
  248.   original_size_total = 0L;
  249.   list_files = 0;
  250.  
  251.   /* open archive file */
  252.   if ((afp = open_old_archive ()) == NULL)
  253.     fatal_error (archive_name);
  254.   if (archive_is_msdos_sfx1 (archive_name))
  255.     skip_msdos_sfx1_code (afp);
  256.  
  257.   /* print header message */
  258.   if (!quiet)
  259.     list_header ();
  260.  
  261.   /* print each file information */
  262.   while (get_header (afp, &hdr))
  263.     {
  264.       if (need_file (hdr.name))
  265.     {
  266.       list_one (&hdr);
  267.       list_files ++;
  268.       packed_size_total += hdr.packed_size;
  269.       original_size_total += hdr.original_size;
  270.     }
  271.  
  272.       if (afp != stdin)
  273.       fseek (afp, hdr.packed_size, SEEK_CUR);
  274.       else
  275.     {
  276.       i = hdr.packed_size;
  277.       while(i--) fgetc(afp);
  278.     }
  279.     }
  280.  
  281.   /* close archive file */
  282.   fclose (afp);
  283.  
  284.   /* print tailer message */
  285.   if (!quiet)
  286.     list_tailer ();
  287.  
  288.   return;
  289. }
  290.  
  291.  
  292.  
  293.  
  294.